=begin

Copyright 2010 Arachne Jericho <arachne.jericho@gmail.com>

This file is part of RubyTea.

RubyTea is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

RubyTea is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with RubyTea.  If not, see <http://www.gnu.org/licenses/>.

=end
# 
# Library for RubyTea. 
#
# Introduces the following globals: 
#
#   $tea_collection: collection of teas
#
# Introduces the following methods:
#
#   anchorize(String) : String - makes a name suitable for an HTML anchor
#   header(String) : void - prints out a header suitable for any RubyTea script
#   footer(String) : void - prints out a footer suitable for any RubyTea script

require 'yaml'
require 'config.rb'

$tea_collection = YAML.load_file YAML_FILE

# Returns a version of the string s with non-word characters 
# and some non-title words (and, with) replaced with '_'. Suitable for 
# creating anchors from names of blenders or teas or translating 
# them to image names.
#
# Multiple '_' in a row are collapsed.
def anchorize(s)
    s1 = s.gsub(/[^-A-Za-z0-9]/, '_').gsub(/with|and/, '_')
    s1.gsub(/_+/, '_')
end
    
# Prints out a header for an HTML file, using the given title. 
# If 'bodypadding' is given, will pad the body. Usually you 
# don't want this for something included in an iframe for, say, 
# a WP blog, but you would for individual CGIs not meant to 
# be iframed. 
def header(title, bodypadding = 0)
    puts <<-END
Content-type: text/html

<?xml version="1.0" encoding="utf-8" ?>
<html>
<head>
  <title>#{title}</title>
    END
    EXTRA_CSS.each do |css| 
        puts %Q(<link type="text/css" href="#{css}" rel="stylesheet" />)
    end
    puts <<-END
  <link type="text/css" href="#{RUBY_TEA_CSS}" rel="stylesheet" />
</head>
<body #{bodypadding != 0 ? %Q(style="padding: #{bodypadding}") : ''}>
#{BEGIN_CONTAINER_HTML}
    END
end

# Prints out a footer for an HTML file
def footer
    puts <<-END
#{END_CONTAINER_HTML}
</body></html>
    END
end
